package 二叉树系列;

import java.util.Deque;
import java.util.LinkedList;

public class InvertTree {
    public TreeNode invertTree(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        if(root==null) return null;
        deque.push(root);
        while(!deque.isEmpty()){
            int len = deque.size();

            for(int i=0;i<len;i++){
                TreeNode treeNode = deque.pollFirst();
                TreeNode temp = null;
                temp = treeNode.left;
                treeNode.left = treeNode.right;
                treeNode.right = temp;
                if(treeNode.left!=null) deque.addLast(treeNode.left);
                if(treeNode.right!=null) deque.addLast(treeNode.right);
            }
        }
        return root;
    }
}
